package com.example.idachuappone.order.frag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.cook.entity.CookResult;
import com.example.idachuappone.cook.item.PayPromptlyActivity;
import com.example.idachuappone.order.OrderFragTwo;
import com.example.idachuappone.order.adapter.MyOrderAdapter;
import com.example.idachuappone.order.adapter.MyOrderAdapter.CallBack;
import com.example.idachuappone.order.item.MyOrderDetailTwoActivity;
import com.example.idachuappone.order.item.SubmitCommentActivity;
import com.example.idachuappone.person.entity.Order;
import com.example.idachuappone.person.method.OrderMethod;
import com.example.idachuappone.person.method.OrderMethod.SetOrderList;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class FragOrderComment extends Fragment implements OnItemClickListener, OnClickListener {
	private List<Order> listOrders;
	private LinearLayout ll_no_network;
	private LinearLayout ll_none;
	private ListView lv_order_list;
	private MyOrderAdapter myOrderAdapter;
	private SwipeRefreshLayout swipeRefreshLayout;
	private TextView tv_data_no;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.frag_order_all, null);
		initView(view);
		checkNet();
		return view;
	}

	private void checkNet() {
		if (CheckUtil.checkNet(getActivity())) {
			getData();
		} else {
			this.ll_no_network.setVisibility(0);
		}
	}

	private void initView(View view) {
		this.ll_no_network = (LinearLayout) view.findViewById(R.id.ll_no_network);
		this.ll_no_network.setOnClickListener(this);
		this.tv_data_no = (TextView) view.findViewById(R.id.tv_data_no);
		this.tv_data_no.setText("\u4f60\u8fd8\u6ca1\u6709\u5f85\u8bc4\u4ef7\u8ba2\u5355");
		this.ll_none = (LinearLayout) view.findViewById(R.id.ll_none);
		this.swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
		this.swipeRefreshLayout.setColorSchemeResources(R.color.color_two_main);
		this.lv_order_list = (ListView) view.findViewById(R.id.lv_order_list_all);
		this.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				FragOrderComment.this.getData();
			}
		});
		this.myOrderAdapter = new MyOrderAdapter(getActivity(), new CallBack() {

			class AnonymousClass_1 implements DialogInterface.OnClickListener {
				private final/* synthetic */String val$id;

				AnonymousClass_1(String str) {
					this.val$id = str;
				}

				@Override
				public void onClick(DialogInterface dialog, int which) {
					FragOrderComment.this.checkNetClear(this.val$id);
				}
			}

			@Override
			public void btnPayOk(String id) {
				Intent intent = new Intent(FragOrderComment.this.getActivity(), PayPromptlyActivity.class);
				intent.putExtra("isorder", true);
				intent.putExtra("isprom", false);
				intent.putExtra("orderid", id);
				FragOrderComment.this.getActivity().startActivity(intent);
			}

			@Override
			public void btnFinish(String id) {
				FragOrderComment.this.checkNetFinish(id);
			}

			@Override
			public void btnComment(CookResult cookResult, String id) {
				Intent intent1 = new Intent(FragOrderComment.this.getActivity(), SubmitCommentActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("cookResult", cookResult);
				bundle.putString("id", id);
				intent1.putExtras(bundle);
				FragOrderComment.this.getActivity().startActivity(intent1);
			}

			@Override
			public void btnClear(String id) {
				new Builder(FragOrderComment.this.getActivity())
						.setMessage("\u4eb2\uff0c\u60a8\u786e\u5b9a\u8981\u53d6\u6d88\u8ba2\u5355\u5417\uff1f")
						.setPositiveButton("\u786e\u5b9a", new AnonymousClass_1(id))
						.setNegativeButton("\u53d6\u6d88", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
							}
						}).create().show();
			}
		});
		this.lv_order_list.setAdapter(this.myOrderAdapter);
		this.lv_order_list.setOnItemClickListener(this);
	}

	public void refresh() {
		OrderFragTwo orderFragTwo = (OrderFragTwo) getParentFragment();
		orderFragTwo.fragOrderAll.getData();
		orderFragTwo.fragOrderPay.getData();
	}

	public void getData() {
		OrderMethod.getOrderList(getActivity(), Constant.PROMPTLY_COMMENT, new SetOrderList() {
			@Override
			public void onSuccess(List<Order> list, String result) {
				FragOrderComment.this.ll_no_network
						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				FragOrderComment.this.listOrders = list;
				FragOrderComment.this.initAdapter();
			}

			@Override
			public void onFail() {
				FragOrderComment.this.ll_no_network.setVisibility(0);
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		if (AppShareData.isRefreshOrderComment) {
			getData();
			AppShareData.isRefreshOrderComment = false;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		Intent intent = new Intent(getActivity(), MyOrderDetailTwoActivity.class);
		intent.putExtra("id", this.listOrders.get(position).getId());
		startActivity(intent);
	}

	private void initAdapter() {
		if (this.listOrders == null || this.listOrders.size() <= 0) {
			this.ll_none.setVisibility(0);
		} else {
			this.ll_none.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		}
		this.myOrderAdapter.setList(this.listOrders);
		this.myOrderAdapter.notifyDataSetChanged();
		this.swipeRefreshLayout.setRefreshing(false);
	}

	private void checkNetFinish(String id) {
		if (CheckUtil.checkNet(getActivity())) {
			Map<String, String> map = new HashMap();
			map.put("id", id);
			NetUtil.post(getActivity(), Constant.PROMPTLY_OK, map, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						if (jsonObject.getInt("code") == 10000) {
							MainToast.show(FragOrderComment.this.getActivity(), "\u6b22\u8fce\u4e0b\u6b21\u4f7f\u7528",
									1);
							FragOrderComment.this.getData();
							FragOrderComment.this.refresh();
							return;
						}
						MainToast.show(FragOrderComment.this.getActivity(), jsonObject.optString("error"), 0);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					MainToast.show(FragOrderComment.this.getActivity(),
							FragOrderComment.this.getResources().getString(R.string.netFail), 0);
				}
			});
			return;
		}
		MainToast.show(getActivity(), getResources().getString(R.string.txt_network), 0);
	}

	private void checkNetClear(String id) {
		if (CheckUtil.checkNet(getActivity())) {
			Map<String, String> map = new HashMap();
			map.put("id", id);
			NetUtil.post(getActivity(), Constant.PROMPTLY_CLEAR, map, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						if (jsonObject.getInt("code") == 10000) {
							AppShareData.isPersonFrag = true;
							MainToast.show(FragOrderComment.this.getActivity(), "\u8ba2\u5355\u5df2\u53d6\u6d88", 1);
							FragOrderComment.this.getData();
							FragOrderComment.this.refresh();
							return;
						}
						MainToast.show(FragOrderComment.this.getActivity(), jsonObject.optString("error"), 0);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					MainToast.show(FragOrderComment.this.getActivity(),
							FragOrderComment.this.getResources().getString(R.string.netFail), 0);
				}
			});
			return;
		}
		MainToast.show(getActivity(), getResources().getString(R.string.txt_network), 0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_no_network /* 2131165232 */:
			checkNet();
		default:
		}
	}
}
